﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BEGDC.CC.BL.Common.Query;
using BEGDC.CC.BL.Workflow;
using BEGDC.CC.Web.UserControls;

namespace ASB.CC.Web.Common
{
    public partial class SubmitOCOApp : System.Web.UI.Page
    {
        protected string FlowID
        {
            get
            {
                return Request.QueryString["FlowID"];
            }
        }

        protected bool isNeedLv2Approval
        {
            get
            {
                if (Request.QueryString["LvStep"] == "Request") return false;

                IQuery query = BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.CC.BL.Common.ServiceConst.QueryService] as IQuery;
                return query.IsNeedNextLvApprover(long.Parse(Request.QueryString["FlowID"]), WorkTransition.OC_Lv3Approval);
            }
        }

        protected bool isNeedLv1Approval
        {
            get
            {
                if (!isNeedLv2Approval) return false;

                IQuery query = BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.CC.BL.Common.ServiceConst.QueryService] as IQuery;
                return query.IsNeedNextLvApprover(long.Parse(Request.QueryString["FlowID"]), WorkTransition.OC_Lv2Approval);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.Btn_Submit.Attributes["onclick"] = "javascript:OnSubmit();";
                this.Btn_Close.Attributes["onclick"] = "javascript:OnClose();";

                if (Request.QueryString["LvStep"] != "Request")
                {
                    SettingNeedLvApproval();
                }

                BindDropDownList();
            }
        }

        private void SettingNeedLvApproval()
        {
            if (this.isNeedLv2Approval)
            {
                WebUtility.BuildAssignmentDropDown(this.dpLv2Approver, BEGDC.CC.BL.Workflow.UserRole.Lv2Approver, null);
                this.dpLv2Approver.Items.Insert(0, new ListItem("Please select"));
            }

            if (this.isNeedLv1Approval)
            {
                WebUtility.BuildAssignmentDropDown(this.dpLv1Approver, BEGDC.CC.BL.Workflow.UserRole.Lv1Approver, null);
                this.dpLv1Approver.Items.Insert(0, new ListItem("Please select"));
            }
        }

        #region Web Form Designer generated code

        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Btn_Submit.Click += new EventHandler(Btn_Submit_Click);
            this.dpLv3Approver.SelectedIndexChanged += new EventHandler(dpLv3Approver_SelectedIndexChanged);
            this.dpLv2Approver.SelectedIndexChanged += new EventHandler(dpLv2Approver_SelectedIndexChanged);
            this.Load += new System.EventHandler(this.Page_Load);
        }

        void dpLv2Approver_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (((CCIdentity)HttpContext.Current.User.Identity).UserInfo.NameEN == dpLv2Approver.SelectedItem.Text && this.isNeedLv1Approval)
            {
                this.trLv1Approver.Visible = true;
                this.dpLv1Approver.SelectedIndex = 0;
                if (this.dpLv1Approver.Items.Count <= 1) this.trLv1ApproverLabel.Visible = true;
            }
            else
            {
                this.trLv1Approver.Visible = false;
                this.trLv1ApproverLabel.Visible = false;
            }
        }

        void dpLv3Approver_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (((CCIdentity)HttpContext.Current.User.Identity).UserInfo.NameEN == dpLv3Approver.SelectedItem.Text && this.isNeedLv2Approval)
            {
                this.trLv2Approver.Visible = true;
                this.dpLv2Approver.SelectedIndex = 0;
                if (this.dpLv2Approver.Items.Count <= 1) this.trLv2ApproverLabel.Visible = true;
            }
            else
            {
                this.trLv2Approver.Visible = false;
                this.trLv1Approver.Visible = false;
                this.trLv2ApproverLabel.Visible = false;
                this.trLv1ApproverLabel.Visible = false;
            }
        }

        #endregion

        void Btn_Submit_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["LvStep"] == "Request")
            {
                string employeeNo = this.dpLv4Approver.SelectedValue;
                if (string.IsNullOrEmpty(employeeNo)) return;

                Response.Write(@"<Script>returnValue='" + employeeNo + "';self.close(); </Script>");
            }
            else if (CheckValue())
            {
                string employeeLv3No = this.dpLv3Approver.Visible ? this.dpLv3Approver.SelectedValue : "";
                string employeeLv2No = this.dpLv2Approver.Visible ? this.dpLv2Approver.SelectedValue : "";
                string employeeLv1No = this.dpLv1Approver.Visible ? this.dpLv1Approver.SelectedValue : "";

                string returnValue = employeeLv3No + "_" + employeeLv2No + "_" + employeeLv1No;
                Response.Write(@"<Script>returnValue='" + returnValue + "';self.close(); </Script>");
            }
            else
            {
                Page.RegisterClientScriptBlock("", "<script>alert('LvApprover step is wrong. 级别审批步骤错误!');</script>");
                return;
            }
        }

        private bool CheckValue()
        {
            if (this.dpLv3Approver.Visible && string.IsNullOrEmpty(this.dpLv3Approver.SelectedValue))
            {
                Page.RegisterClientScriptBlock("", "<script>alert('Please select the Lv3 Approver. 请选择3级审批者!');</script>");
                return false;
            }

            if (this.dpLv2Approver.Visible && string.IsNullOrEmpty(this.dpLv2Approver.SelectedValue))
            {
                Page.RegisterClientScriptBlock("", "<script>alert('Please select the Lv2 Approver. 请选择2级审批者!');</script>");
                return false;
            }

            if (this.dpLv1Approver.Visible && string.IsNullOrEmpty(this.dpLv1Approver.SelectedValue))
            {
                Page.RegisterClientScriptBlock("", "<script>alert('Please select the Lv1 Approver. 请选择1级审批者!');</script>");
                return false;
            }
            return true;
        }

        private void BindDropDownList()
        {
            if (Request.QueryString["LvStep"] == "Request")
            {
                this.trLv4Approver.Visible = true;
                WebUtility.BuildAssignmentDropDown(this.dpLv4Approver, BEGDC.CC.BL.Workflow.UserRole.Lv4Approver, null);
                if (this.dpLv4Approver.Items.Count <= 0) this.trLv4ApproverLabel.Visible = true;
                this.dpLv4Approver.Items.Insert(0, new ListItem("Please select"));
            }
            else if (Request.QueryString["LvStep"] == WorkTransition.OC_Lv4Approval)
            {
                this.trLv3Approver.Visible = true;
                WebUtility.BuildAssignmentDropDown(this.dpLv3Approver, BEGDC.CC.BL.Workflow.UserRole.Lv3Approver, null);
                if (this.dpLv3Approver.Items.Count <= 0) this.trLv3ApproverLabel.Visible = true;
                this.dpLv3Approver.Items.Insert(0, new ListItem("Please select"));
            }
            else if (Request.QueryString["LvStep"] == WorkTransition.OC_Lv3Approval)
            {
                this.trLv2Approver.Visible = true;
                WebUtility.BuildAssignmentDropDown(this.dpLv2Approver, BEGDC.CC.BL.Workflow.UserRole.Lv2Approver, null);
                if (this.dpLv2Approver.Items.Count <= 0) this.trLv2ApproverLabel.Visible = true;
                this.dpLv2Approver.Items.Insert(0, new ListItem("Please select"));
            }
            else if (Request.QueryString["LvStep"] == WorkTransition.OC_Lv2Approval)
            {
                this.trLv1Approver.Visible = true;
                WebUtility.BuildAssignmentDropDown(this.dpLv1Approver, BEGDC.CC.BL.Workflow.UserRole.Lv1Approver, null);
                if (this.dpLv1Approver.Items.Count <= 0) this.trLv1ApproverLabel.Visible = true;
                this.dpLv1Approver.Items.Insert(0, new ListItem("Please select"));
            }
            else
            {
                Page.RegisterClientScriptBlock("", "<script>alert('LvApprover step is wrong. 级别审批步骤错误!');</script>");
                return;
            }
        }
    }
}
